package Servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Flights.Flight;
import Flights.Flights;
import Flights.Passenger;
import Flights.Passengers;

/**
 * Servlet implementation class listFlightServlet
 */
@WebServlet("/listFlightServlet")
public class listFlightServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public listFlightServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
		ServletContext ctx = getServletContext();
		HttpSession session = request.getSession();
		
		PrintWriter out = response.getWriter(); 
		
		String listPassenger = request.getParameter("listPassenger");
		String listFlight = request.getParameter("listFlight");
		
		String client = request.getParameter("client");
		if(client==null){
			client="web";
		}
		
		if(listPassenger!=null){
			Flights flights = (Flights) session.getAttribute("flights");
			if(flights==null){
				flights = new Flights(ctx.getRealPath("") + "/listaLetova/flights.txt");
			}
			
			for(Flight f : flights.getAllFlights()){
				if(f.getIdLeta().equals(listPassenger)){
					flights.setFlight(f);
				}
			}
			
			if(flights.getFlight()==null){
				out.println("Ne postoji let: " + listPassenger);
				out.close();
				return;
			}
			
			Passengers passengers = new Passengers(ctx.getRealPath("") + "/listaLetova/" + listPassenger + ".txt");
				
			session.setAttribute("passengers", passengers);
			
			if(client.equals("CommandPrompt")){
				int preostaliBrojMesta = flights.getFlight().getBrojPutnika()-passengers.getPassengers().size();
				
				String f = "Let: " + flights.getFlight().getIdLeta() + ", od " 
						+ flights.getFlight().getPolaziAerodrom() + ", do " 
						+ flights.getFlight().getOdrediste() + ", vreme polaska: " 
						+ flights.getFlight().getVremePolaskaString() + ", vreme dolaska: " 
						+ flights.getFlight().getVremeDolaskaString() + ", broj slobodnih mesta: "
						+ String.valueOf(preostaliBrojMesta) + "/" + flights.getFlight().getBrojPutnika();
				
				out.println(f);

				out.close();
			} else {
				if(flights.getFlight().getBrojPutnika()-passengers.getPassengers().size() <=0){
					flights.getFlight().setSlobodnaMesta(false);
				} else {
					flights.getFlight().setSlobodnaMesta(true);
				}
				
				RequestDispatcher disp = getServletContext().getRequestDispatcher("/list_flight.faces");
				disp.forward(request, response);
			}
			return;
		} else if(listFlight!=null){
			Passengers passengers = (Passengers) session.getAttribute("passengers");
			
			if(!listFlight.equals("flights")){
				if(passengers!=null){
					for(Passenger p : passengers.getPassengers()){
						if(p.getJMBG().equals(listFlight)){
							passengers.setPassenger(p);
						}
					}
				}
				File f = new File(ctx.getRealPath("") + "/listaPutnika/" + listFlight + ".txt");
				if(!f.exists()){
					out.println("Ne postiji putnik sa JMBGom: " + listFlight);
					out.println("");
					out.close();
					return;
				}
				
				Flights flights = new Flights(ctx.getRealPath("") + "/listaPutnika/" + listFlight + ".txt");
				
				session.setAttribute("flights", flights);
				session.setAttribute("passengers", passengers);
				
				if(client.equals("CommandPrompt")){
					for(Flight fl : flights.getAllFlights()){
						String flightString = "Let: " + fl.getIdLeta() + ", od " 
								+ fl.getPolaziAerodrom() + ", do " 
								+ fl.getOdrediste() + ", vreme polaska: " 
								+ fl.getVremePolaskaString() + ", vreme dolaska: " 
								+ fl.getVremeDolaskaString();
						
						out.println(flightString);
					}
					out.println("");
					out.close();
				} else {
					RequestDispatcher disp = getServletContext().getRequestDispatcher("/list_passenger.faces");
					disp.forward(request, response);
				}
				return;
			} else {
				Flights flights = new Flights(ctx.getRealPath("") + "/listaLetova/" + listFlight + ".txt");
				
				session.setAttribute("flights", flights);
				
				if(client.equals("CommandPrompt")){
					
					for(Flight fli : flights.getAllFlights()){
						Passengers pas = new Passengers(ctx.getRealPath("") + "/listaLetova/" + fli.getIdLeta() + ".txt");
						
						int preostaliBrojMesta = fli.getBrojPutnika()-pas.getPassengers().size();
						
						String f = "Let: " + fli.getIdLeta() + ", od " 
								+ fli.getPolaziAerodrom() + ", do " 
								+ fli.getOdrediste() + ", vreme polaska: " 
								+ fli.getVremePolaskaString() + ", vreme dolaska: " 
								+ fli.getVremeDolaskaString() + ", broj slobodnih mesta: "
								+ String.valueOf(preostaliBrojMesta) + "/" + fli.getBrojPutnika();
						out.println(f);
					}
					out.println("");
					out.close();
				} else {
					RequestDispatcher disp = getServletContext().getRequestDispatcher("/main.faces");
					disp.forward(request, response);
				}
				return;
			}
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
